from PyQt5.QtWidgets import QWidget,QFileDialog,QSizePolicy
from PyQt5.QtGui import QDoubleValidator,QIcon
from PyQt5.QtCore import QUrl,QObject,pyqtSignal,pyqtProperty,QTimer,pyqtSlot,Qt,QEvent
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtWebEngineWidgets import QWebEngineView
from ui.uiDisplay import Ui_Display
import pandas as pd

#WebChannel注册的中间件中signal在GUI主循环开始后才会触发

IconDir="./icon/"

class WebPageMiddle(QObject):
    textChanged=pyqtSignal(str)
    threshChanged=pyqtSignal(str)
    minmaxClicked=pyqtSignal(int)
    def __init__(self):
        super().__init__()
        self._text=""
        self._thresh=""

    def getText(self):
        return self._text

    def setText(self,text):
        self._text=text
        self.textChanged.emit(text)

    def getThresh(self):
        return self._thresh

    def setThresh(self,thresh):
        self._thresh=thresh
        self.threshChanged.emit(thresh)

    @pyqtSlot(int)
    def webMinmaxClicked(self,isMaximum):
        self.minmaxClicked.emit(isMaximum)
    text=pyqtProperty(str,fget=getText,fset=setText)
    thresh=pyqtProperty(str,fget=getThresh,fset=setThresh)

class DisplayWidget(QWidget,Ui_Display):
    showMaximum=pyqtSignal(int)
    def __init__(self,parent=None):
        super(DisplayWidget, self).__init__(parent)
        self.setupUi(self)
        self.initUi()
        self.middle = WebPageMiddle()
        self.channel = QWebChannel()
        self.channel.registerObject("middle", self.middle)
        self.connectSignals()
        self.timer = None
        # self.installEventFilter(self)

    def initUi(self):
        self.browser = QWebEngineView()  # 不能再designer里拖拽添加，否则setWebChannel失败，原因未知
        self.browser.load(QUrl("file:///chart/index.html"))
        self.hl.addWidget(self.browser)
        self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowSystemMenuHint|Qt.WindowMinMaxButtonsHint)
        self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

    def connectSignals(self):
        self.middle.minmaxClicked.connect(lambda x:self.showMaximum.emit(x))

    # def onDataSpeedChanged(self,seconds):
    #     if self.timer is None:
    #         return
    #     self.timer.setInterval(seconds*1000)
    #
    # def beginSendData(self):
    #     '''
    #     调用此函数前，先设置nextRowCallback函数
    #     '''
    #     self.timer = QTimer()
    #     self.onDataSpeedChanged(1)
    #     self.timer.timeout.connect(self.sendData)
    #     self.timer.start()

    def sendData(self):
        data=self.nextRowCallback(self.property('tableName'))
        if not data is None:
            self.middle.text=data

    def showMaximized(self):
        self.geoBefore=self.geometry()
        self.browser.page().runJavaScript("resizeLater(500)")
        super(DisplayWidget, self).showMaximized()

    def showNormal(self):
        self.setGeometry(self.geoBefore)
        self.browser.page().runJavaScript("resizeLater(500)")
        super(DisplayWidget, self).showNormal()

    def resizeEvent(self, event):
        self.browser.page().runJavaScript("resize()")
        super().resizeEvent(event)

    def resizeLater(self,milliseconds):
        self.browser.page().runJavaScript("resizeLater(%d)" % milliseconds)

    # def chooseTestFile(self):
    #     fileName=r"C:\WorkSpace\zhushuizhan\data\一号泵.csv"#fileType=QFileDialog.getOpenFileName(self,"选择测试文件","./","csv 文件 (*.csv)")
    #     if fileName=='':
    #         return
    #     self.leTestFilePath.setText(fileName)
    #     self.loadTestFile(fileName)
    #     self.beginSendData()
    #
    # def chooseNetModel(self):
    #     path=QFileDialog.getExistingDirectory(self,"选择神经网络模型","./")
    #     if path=='':
    #         return
    #     self.leNetModelPath.setText(path)

if __name__ == '__main__':
    import sys
    from PyQt5.QtWidgets import QApplication
    app = QApplication(sys.argv)
    exe = DisplayWidget()
    exe.show()
    exe.browser.page().setWebChannel(exe.channel)#不能再类内部setWebChannel，原因不明，猜测需要在QApplication创建后使用

    sys.exit(app.exec_())